﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasSpecialSkillTypeDAL : IMasSpecialSkillTypeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasSpecialSkillTypeDAL()
        {

        }

        public IList<MasSpecialSkillTypeInfo> ListSpecialSkillTypes(MasSpecialSkillTypeInfo skillInfo)
        {
            string strSQL;

            List<MasSpecialSkillTypeInfo> infoList = new List<MasSpecialSkillTypeInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", skillInfo.Sequence);

                strSQL = "SELECT SKILL_TYPE_SEQ, SKILL_TYPE_NAME, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_SPECIAL_SKILL_TYPES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (skillInfo.Sequence > 0 ? " AND SKILL_TYPE_SEQ = @Seq" : "")
                    + " ORDER BY SKILL_TYPE_NAME";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasSpecialSkillTypeInfo Info = new MasSpecialSkillTypeInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? null : (Nullable<int>)rdr.GetInt32(4)
                            , rdr.IsDBNull(5) ? null : (Nullable<int>)rdr.GetInt32(5));
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Insert(MasSpecialSkillTypeInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@Name", info.Name);
                dbManager.AddParameters(1, "@Remarks", info.Remarks);
                dbManager.AddParameters(2, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@CreatedBy", info.CreatedBy);
                dbManager.AddParameters(4, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_SPECIAL_SKILL_TYPES(SKILL_TYPE_NAME, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Name, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasSpecialSkillTypeInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@Seq", info.Sequence);
                dbManager.AddParameters(1, "@Name", info.Name);
                dbManager.AddParameters(2, "@Remarks", info.Remarks);
                dbManager.AddParameters(3, "@UpdatedBy", info.UpdatedBy);
                dbManager.AddParameters(4, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_SPECIAL_SKILL_TYPES"
                           + " SET SKILL_TYPE_NAME = @Name"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE SKILL_TYPE_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE MAS_SPECIAL_SKILL_TYPES"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE SKILL_TYPE_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
